<template>
  <client-only>
    <div class="container_container">
      <!-- 搜索 -->
      <!-- 头部导航栏 -->
      <div class="seacher">
        <a href="/" class="tab">首页</a>
        <a href="/gameCategory" class="tab">分类</a>
        <a href="/personCenter" class="tab">个人中心</a>
        <input
          type="text"
          placeholder="搜索游戏"
          class="ipt"
          v-model.trim="gameName"
          @keydown.enter="search"
        />
        <button class="btn" @click="search">🔍</button>
      </div>
      <!-- 搜索结束 -->
      <!-- 换灯片开始 -->
      <div class="banner">
        <div class="banner_container">
          <p>精选和推荐</p>
          <div class="banner_img_box">
            <a
              :href="`/gameCategory/${showBanner.id}`"
              @mouseenter="stopTime"
              @mouseleave="continueTime"
            >
              <img :src="bigBannerImg" ref="bigImg" :alt="showBanner.name" />
              <div class="img_box_txt">
                <div class="img_tit">{{ showBanner.name }}</div>
                <div class="img_smImg">
                  <img
                    v-for="(img, index) in showBanner.bannersImg"
                    :key="index"
                    :src="img"
                    alt="商品轮播图"
                    @mouseenter="
                      bigBannerImg = img;
                      $refs.bigImg.style = '';
                    "
                    @mouseleave="
                      bigBannerImg = showBanner.firstImg;
                      $refs.bigImg.style = 'animation: show .3s';
                    "
                  />
                </div>
                <div class="img_ifo">
                  立即预购 <br />
                  <span>热销商品</span>
                </div>
                <div class="img_price">
                  <span>￥{{ showBanner.price }}</span>
                  <img
                    src="http://www.xclove.xyz/gameMallImg/icon_platform_win.png"
                    style="opacity: 0.3; float: right; margin-right: 10px"
                    alt=""
                  />
                </div>
              </div>
            </a>
            <div
              class="arrows_left"
              @click="toggleBanner(1)"
              @mouseenter="stopTime"
              @mouseleave="continueTime"
            ></div>
            <div
              class="arrows_right"
              @click="toggleBanner(2)"
              @mouseenter="stopTime"
              @mouseleave="continueTime"
            ></div>
          </div>
          <div class="banner_dot">
            <div
              v-for="(item, index) in bannerImg"
              :key="index"
              :class="{ show_banner_dot: index == showBannerIndex }"
              @click="toggleBanner(0, index)"
              @mouseenter="stopTime"
              @mouseleave="continueTime"
            ></div>
          </div>
        </div>
      </div>
      <!-- 轮播图结束 -->

      <!-- 精选 -->
      <p style="text-align: center; color: #c6d4df">精选特惠</p>
      <div class="selected-offers">
        <a
          class="box"
          v-for="item in discountList"
          :href="`/gameCategory/${item.id}`"
          :key="item.id"
        >
          <img :src="item.cover" :alt="item.name" width="100%" />
          <div class="none">
            <img :src="item.bannersImg[0]" :alt="item.name" width="100%" />
            <div class="title">
              <h2>{{ item.name }}</h2>
              <div>
                <span class="smTit">发行时间: </span>
                {{ item.issueDate }}
              </div>
              <div>
                <span class="smTit">游戏标签: </span>
                <span
                  v-for="smTit in item.tab"
                  :key="smTit"
                  class="smContent"
                  >{{ smTit }}</span
                >
              </div>
            </div>
            <button class="goInfo" @click="goInfo(item.id)">查看详情</button>
          </div>
        </a>
      </div>
      <!-- 精选结束 -->
      <!-- 选项卡开始 -->
      <div class="menu">
        <div class="box">
          <ul>
            <li v-for="(item, i) in list" :key="i">
              <a
                href.prevent="#"
                :class="[{ active: activeType == item.type }]"
                @click="tab(item.type)"
                >{{ item.name }}</a
              >
            </li>
          </ul>
        </div>
        <div id="container">
          <div class="left">
            <a
              class="left_content"
              :class="{ leftHov: gameList.id == item.id }"
              v-for="item in showData"
              :href="`/gameCategory/${item.id}`"
              :key="item.id"
              @mouseenter="getList(item.id)"
            >
              <img :src="item.firstImg" :alt="item.name" width="30%" />
              <div class="left_font">
                <div
                  class="font_tit"
                  :class="{ leftTitHov: gameList.id == item.id }"
                >
                  {{ item.name }}
                </div>
                <img
                  src="http://www.xclove.xyz/gameMallImg/icon_platform_win_dark.png"
                  style="opacity: 0.3"
                  alt=""
                />
                <div>
                  <span
                    v-for="(smTit, index) in item.tab"
                    :key="smTit"
                    class="smContent"
                    >{{ smTit
                    }}<span v-if="index + 1 != item.tab.length">，</span>
                  </span>
                </div>
              </div>
              <div
                class="price"
                :class="{ leftTitHov: gameList.id == item.id }"
              >
                ￥{{ item.price.toFixed(2) }}
              </div>
            </a>
          </div>
          <!-- 右侧图片显示 -->
          <div class="rightImg" v-if="gameList">
            <div class="right_tit">{{ gameList.name }}</div>
            <div class="right_info">
              发行时间：<br />{{ gameList.issueDate }}
            </div>
            <div class="right_tab">
              <span v-for="smTit in gameList.tab" :key="smTit">{{
                smTit
              }}</span>
            </div>
            <div class="box">
              <img
                v-for="(item, i) in gameList.bannersImg"
                :key="i"
                :src="item"
                alt=""
                class="img"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </client-only>
</template>

<script>
import "~/assets/css/index_content.css";
import indexApi from "@/api/index";
import discountApi from "@/api/discount";
import gameInfoApi from "@/api/gameInfo";
import Cookies from "js-cookie";

export default {
  data() {
    return {
      gameName: "", //搜索游戏名称
      bannerImg: [], //所有的轮播图
      showBannerIndex: 0, //当前展示的轮播图索引
      showBanner: {
        firstImg: "",
        bannersImg: [],
        name: "",
        price: "",
      }, //当前所展示的轮播图商品信息
      bigBannerImg: "", // 当前所展示的轮播图
      bannerTime: "", //轮播图自动播放
      data: [], //获取数据
      showData: [], //获取数据
      discountList: {}, //优惠商品
      title: "",
      list: [
        {
          name: "新品与热门商品",
          type: 1,
        },
        {
          name: "热销商品",
          type: 2,
        },
        {
          name: "优惠商品",
          type: 4,
        },
      ],
      activeType: 1,
      gameList: {
        //根据id获取信息
        id: "",
      },
    };
  },
  created() {
    this.init();
    // 获取优惠商品
    this.discount();
    // 获取轮播图
    this.getBannerImg();
    // 轮播图自动播放
    this.continueTime();
  },
  // 监听tab切换
  watch: {
    // 底部商品
    activeType(val) {
      this.dataFilter(this.activeType);
    },
  },
  methods: {
    // 搜索商品
    search() {
      Cookies.set("gameName", this.gameName);
      this.$router.push({ path: "/search" });
      this.$router.push({ path: "/search" });
    },
    // 获取所有商品
    init() {
      indexApi.getAllGame().then((res) => {
        this.data = res.data;
        // 对商品进行过滤
        this.dataFilter(this.activeType);
      });
    },
    // 获取banner图
    getBannerImg() {
      indexApi.getBanner().then((res) => {
        this.bannerImg = res.data;
        // 过滤轮播图
        this.filterBanner();
      });
    },
    // 当前显示的轮播图
    filterBanner() {
      // 获取当前所展示的轮播图信息
      this.showBanner = this.bannerImg[this.showBannerIndex];
      // 获取轮播图第一张图片
      this.bigBannerImg = this.showBanner.firstImg;
    },
    // 临界值判断
    crit() {
      if (this.showBannerIndex >= this.bannerImg.length) {
        this.showBannerIndex = 0;
      } else if (this.showBannerIndex < 0) {
        this.showBannerIndex = this.bannerImg.length - 1;
      }
    },
    // 切换轮播图
    toggleBanner(type, showIndex) {
      // type为1则是向前切换
      // type为2则是向后切换
      if (type == 1) {
        this.showBannerIndex--;
      } else if (type == 2) {
        this.showBannerIndex++;
      } else {
        this.showBannerIndex = showIndex;
      }
      // 临界值判断
      this.crit();
      // 切换轮播
      this.filterBanner();
    },
    // 轮播图自动播放
    continueTime() {
      this.bannerTime = setInterval(() => {
        this.toggleBanner(2);
      }, 2000);
    },
    // 轮播图停止播放
    stopTime() {
      clearInterval(this.bannerTime);
    },
    // 对商品进行过滤
    dataFilter(typeId) {
      this.showData = this.data.filter((item) => item.goodType == typeId);
      this.getList(this.showData[0].id);
    },
    // 去查看详情
    goInfo(gameId) {
      this.$router.push(`/gameCategory/${gameId}`);
    },
    // 获取优惠商品
    discount() {
      discountApi.getList().then((res) => {
        this.discountList = res.data;
      });
    },
    btn() {
      location.href = "/search";
    },
    // tab切换
    tab(typeId) {
      this.activeType = typeId;
    },
    // 获取右侧商品信息
    getList(id) {
      this.gameList = this.showData.filter((item) => item.id == id)[0];
    },
  },
};
</script>
<style scoped>
</style>